package phoenics.apeak.crawl;

import java.net.HttpCookie;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Service;

import com.lmax.disruptor.dsl.Disruptor;

import phoenics.apeak.bus.config.AppConfig;
import phoenics.apeak.crawl.param.Param;

@Service
public class DocumentPublisher {

	@Autowired
	private Disruptor<Document> disruptor;
	public DocumentPublisher() {
	}
	
	public void toPublishEvent(Class<? extends Extractor> extractorClass,String url){
		this.toPublishEvent(extractorClass,url,null,null);
	}
	public void toPublishEvent(Class<? extends Extractor> extractorClass,String url,Param params){
		 this.toPublishEvent(extractorClass,url,params,null);
	}
	private void toPublishEvent(Class<? extends Extractor> extractorClass,String url,Param params,HttpCookie cookie){
		disruptor.publishEvent((event,sequence)->{
			event.setExtractorClass(extractorClass);
			event.setUrl(url);
			event.setParams(params);
			event.setCookie(cookie);
		});
	}
	
	@SuppressWarnings("resource")
	public static void main(String[] args){
		ApplicationContext applicationContext=new AnnotationConfigApplicationContext(AppConfig.class);
		//DocumentPublisher dp=applicationContext.getBean(DocumentPublisher.class);
		//dp.toPublishEvent(extractorClass, url, params, cookie);
/*		@SuppressWarnings("unchecked")
		 Disruptor<Document> disruptordd=( Disruptor<Document>)applicationContext.getBean("disruptor");
		//DocumentTranslator dd=new DocumentTranslator("aaa",3);
		Integer mm=new Integer(3);
		Iii iii=new Iii();
		System.out.println("==1=="+mm.toString());
		System.out.println("==2=="+iii.toString());
		//disruptordd.publishEvent(new DocumentTranslator("aaa",mm,iii));
		//disruptordd.publishEvent(new DocumentTranslator("aaa",mm,iii));
		disruptordd.publishEvent(new DocumentTranslator(){

			@Override
			public void translateToDocument(Document event) {
				// TODO Auto-generated method stub
				
			}
			
		}
		);
		disruptordd.shutdown();*/
		
	}

}
